﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using uz.control.CustomControl;

namespace uz.control.IPO
{
    public class IPOField :TNB_Label
    {
        private string _Type = "string";
        public string Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        private string _Note = "说明";

        public string Note
        {
          get { return _Note; }
          set { _Note = value; }
        }

        private bool _IsNew = true;

        public bool IsNew
        {
            get { return _IsNew; }
            set { _IsNew = value; }
        }

        public IPOField()
        {
            ToolBar.AddCommand(CMD_EDIT_STR, "编辑（F2）");
            ToolBar.AddCommand(CMD_DEL_STR, "删除（F3）");
            ToolBar.AddCommand(CMD_ADD_STR, "添加参数（F4）");

            this.ID = "parameter";
            this.Name = "参数";
        }

        public override void DrawNodeText(PaintEventArgs e, Brush brush, Rectangle Bounds)
        {
            SizeF sf = SizeF.Empty;
            string str = (Index+1).ToString().PadLeft((Parent.Nodes.Count-1).ToString().Length,'0');
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            float xOffset = Bounds.Left + HandleAreaWidth + GetIndentWidth() + 0;
            float yOffset = Bounds.Top +  5;
            e.Graphics.DrawString(str, UzTreeView.Font, brush, xOffset,yOffset);

            xOffset += sf.Width;
            str = "" + this.Type.Trim() + " ";
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, Brushes.Blue, xOffset, yOffset);
                        
            xOffset += sf.Width;
            str = this.ID.Trim();
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, brush, xOffset, yOffset);

            xOffset += sf.Width;
            str = "//" + this.Name.Trim() + "";
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, Brushes.Green, xOffset, yOffset);

            xOffset += sf.Width;
            str = "";
            if (Note != "")
            {
                str = "（" + Note + "）";
            }
            e.Graphics.DrawString(str, UzTreeView.Font, Brushes.Green, xOffset, yOffset);
        }

        public override bool EndEdit(UzTextBox txt)
        {
            string strData = txt.Text.Trim();
            if (strData == "")
            {
                ShowMessage("必须输入参数的ID和名称！");
                txt.SelectAll();
                return false;
            }


            string[] strAry = strData.Split(' ');
            string id = "";
            string name = "";
            string type = "";
            string note = "";

            foreach (string str in strAry)
            {
                if (type == "")
                {
                    type += str.Trim();
                }
                else if(id == "")
                {
                    id += str.Trim();
                }
                else if (name == "")
                {
                    name += str.Trim();
                }
                else
                {
                    note += str.Trim();
                }
            }

            if (type == "" || id == "" || name == "")
            {
                ShowMessage("格式不正确！");
                txt.SelectAll();
                return false;
            }

            this.Type = type;
            this.ID = id;
            this.Name = name;
            this.Note = note;

            IsNew = false;

            UzTreeNode node = GetNextBrotherNode();

            if (node == null)
            {
                Add();
            }
            else
            {
                node.IsEdit = true;
            }

            return true;
        }

        public override bool IsEdit
        {
            get
            {
                return base.IsEdit;
            }
            set
            {
                base.IsEdit = value;
                if (value == false)
                {
                    if (IsNew == true)
                    {
                        Delete();
                    }
                }
            }
        }

        public override void BeginEdit(UzTextBox txt)
        {
            txt.Text = Type + " " + ID + " " + Name + " " + Note;
            txt.SelectAll();
        }

        private void ShowMessage(string msg)
        {
            string str = "";
            str += "  输入格式如下：\n";
            str += "  参数类型+空格+参数ID+空格+参数名称+空格+参数说明\n";
            str += "  （例：string name 姓名 用来存储员工姓名）";


            str = msg + "\n" + str;

            MessageBox.Show(str);
        }

        public override void Delete()
        {
            UzTreeNode node = null;
            if (IsNew == false)
            {
                node = GetNextVisibleNode();
                if (node == null)
                {
                    node = GetPrevVisibleNode();
                }
            }
            else
            {
                node = GetPrevVisibleNode();
                if (node == null)
                {
                    node = GetNextVisibleNode();
                }
            }

            Parent.Nodes.RemoveAt(Index);

            if (node != null)
            {
                UzTreeView.SelectedNode = node;
            }

        }
    }
}
